package org.hxyjs.dao.impl;

import org.hxyjs.dao.ActivetyrecordDao;
import org.hxyjs.entity.Activetyrecord;
import org.hxyjs.mapper.ActivetyrecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("activetyrecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ActivetyrecordDaoImpl implements ActivetyrecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ActivetyrecordMapper activetyrecordMapper;

    @Override
    public List<Activetyrecord> getActivetyrecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) activetyrecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return activetyrecordMapper.getActivetyrecordByPage(map);
    }


    public List<Activetyrecord> getAllActivetyrecords() throws Exception {
        return activetyrecordMapper.selectByExample(null);
    }

    @Override
    public Activetyrecord getActivetyrecordByAfno(String afno) throws Exception {
        return activetyrecordMapper.selectByPrimaryKey(afno);
    }


    @Override
    public Integer deleteActivetyrecordByAfno(String afno) {
        return activetyrecordMapper.deleteByPrimaryKey(afno);
    }

    @Override
    public Integer updateActivetyrecordByAfno(Activetyrecord activetyrecord) {

        return activetyrecordMapper.updateByPrimaryKey(activetyrecord);
    }

    @Override
    public Integer addActivetyrecord(Activetyrecord activetyrecord) {
        activetyrecord.setAfno(initId());
        return activetyrecordMapper.insertSelective(activetyrecord);
    }

    public String initId() {
        String maxAfno = activetyrecordMapper.getMaxAfno();
        return GenNumTools.initId("490032020100117", 4, maxAfno);
    }

}